package com.jingdianjichi.wx.controller;

import com.jingdianjichi.wx.handler.WxChatMsgFactory;
import com.jingdianjichi.wx.handler.WxChatMsgHandler;
import com.jingdianjichi.wx.utils.MessageUtil;
import com.jingdianjichi.wx.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/*
 * 微信回调controller
 * */
@RestController
@Slf4j
public class CallbackController {

    private static final String token = "zhangmingxing";

    @Resource
    private WxChatMsgFactory wxChatMsgFactory;

    @RequestMapping("/test")
    public String test() {
        return "helloWorld";
    }

    /*
     * 回调消息接受
     * */
    @GetMapping("/callback")
    public String callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        log.info("接受微信发来的请求，signature：{}，timestamp：{}，nonce：{}，echostr：{}",
                signature, timestamp, nonce, echostr);

        String sha1 = SHA1.getSHA1(token, timestamp, nonce, "");
        if (sha1.equals(signature)) {
            return echostr;
        }
        return "unKnow";
    }

    @PostMapping(value = "/callback", produces = "application/xml;charset=UTF-8")
    public String callback(@RequestBody String requestBody,
                           @RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam(value = "msg_signature", required = false) String msgSignature) {
        log.info("接受到微信消息：requestBody:{},signature:{},timestamp:{},nonce:{},msgSignature:{}",
                requestBody, signature, timestamp, nonce, msgSignature);
        Map<String, String> handlerMap = MessageUtil.parseXml(requestBody);
        String msgType = handlerMap.get("MsgType");
        String event = handlerMap.get("Event") == null ? "" : handlerMap.get("Event");
        log.info("msgType:{},event:{}", msgType, event);

        StringBuilder sb = new StringBuilder();
        sb.append(msgType);
        if(!StringUtils.isEmpty(event)){
            sb.append(".");
            sb.append(event);
        }
        String msgTypeKey = sb.toString();
        WxChatMsgHandler wxChatMsgHandler = wxChatMsgFactory.getHandlerMsgType(msgTypeKey);
        if(Objects.isNull(wxChatMsgHandler)){
            return "unknown";
        }
        String replyContent = wxChatMsgHandler.dealMsg(handlerMap);
        return replyContent;
    }
//    String fromUserName = parseXml.get("FromUserName");
//    String toUserName = parseXml.get("ToUserName");
//    String content = "<xml>\n" +
//            "  <ToUserName><![CDATA[" + toUserName + "]]></ToUserName>\n" +
//            "  <FromUserName><![CDATA[" + fromUserName + "]]></FromUserName>\n" +
//            "  <CreateTime>1348831860</CreateTime>\n" +
//            "  <MsgType><![CDATA[text]]></MsgType>\n" +
//            "  <Content><![CDATA[学java的小张想要月薪过万]]></Content>\n" +
//            "</xml>\n";
}
